﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGen
{
    class BllFileGenerator : CSharpFileGenerator
    {
        public BllFileGenerator(string NameSpace, string Class, string Dal, DbSettings DbSettings)
        {
            this.NameSpace = NameSpace;
            this.Class = Class;
            this.Dal = Dal;
            this.DbSettings = DbSettings;
        }

        string NameSpace = string.Empty;
        string Class = string.Empty;
        string Dal = string.Empty;
        DbSettings DbSettings;

        protected override List<string> GetUsingNameSpaces()
        {
            return new List<string>
            {
                "System",
                "System.Collections",
                "System.Collections.Generic",
                "System.ComponentModel",
                "System.Reflection",
                "JUtil",
                "JUtil.Extensions",
                "JUtil.Web",
                "JUtil.ResourceManagement",
                "System.Data"
            };
        }

        protected override string GetDeclareNameSpaces(){ return NameSpace; }

        protected override void ExportClass(StringBuilder sb)
        {
            /*
                [DataObject(true)]
                public class BLL_FESTIVAL_ACTIVITY : BLL<FESTIVAL_ACTIVITY>
                {
                }
             */
            sb.Append(IndentString("[DataObject(true)]"));
            sb.Append("\n");

            string ClassPresentation = string.Format("public class {0} : BLL<{1}>", Class, Dal);

            sb.Append(IndentString(ClassPresentation));
            sb.Append("\n");

            sb.Append(StartBlock);

            ExportGetOrderBy(sb);
            ExportGetSelectSQLNoOrderBy(sb);
            ExportGetSelectDetailsVwSQL(sb);
            ExportGetInsertSQL(sb);
            ExportGetUpdateSQL(sb);
            ExportGetDeleteSQL(sb);
            ExportCreateDbObject(sb);

            sb.Append(EndBlock);
        }

        #region ExportGetOrderBy
        private void ExportGetOrderBy(StringBuilder sb)
        {
            /*
            #region GetOrderBy
            protected override string GetOrderBy()
            {
                return SqlFile.GetSQL("GetOrderBy");
            }
            #endregion
             */
            sb.Append(IndentString("#region GetOrderBy"));
            sb.Append("\n");

            sb.Append(IndentString("protected override string GetOrderBy()"));
            sb.Append("\n");

            sb.Append(StartBlock);

            sb.Append(IndentString("return SqlFile.GetSQL(\"GetOrderBy\");"));
            sb.Append("\n");

            sb.Append(EndBlock);

            sb.Append(IndentString("#endregion"));
            sb.Append("\n\n");
        }
        #endregion
        #region ExportGetSelectSQLNoOrderBy
        private void ExportGetSelectSQLNoOrderBy(StringBuilder sb)
        {
            /*
            #region GetSelectSQLNoOrderBy
            protected override string GetSelectSQLNoOrderBy(Hashtable Parameters)
            {
                string sql = SqlFile.GetSQL("GetSelectSQLNoOrderBy");
                return sql;
            }
            #endregion
             */
            sb.Append(IndentString("#region GetSelectSQLNoOrderBy"));
            sb.Append("\n");

            sb.Append(IndentString("protected override string GetSelectSQLNoOrderBy(Hashtable Parameters)"));
            sb.Append("\n");

            sb.Append(StartBlock);

            sb.Append(IndentString("throw new NotImplementedException();"));
            sb.Append("\n\n");

            sb.Append(IndentString("string sql = SqlFile.GetSQL(\"GetSelectSQLNoOrderBy\");"));
            sb.Append("\n");
            sb.Append(IndentString("return sql;"));
            sb.Append("\n");

            sb.Append(EndBlock);

            sb.Append(IndentString("#endregion"));
            sb.Append("\n\n");
        }
        #endregion
        #region ExportGetSelectDetailsVwSQL
        private void ExportGetSelectDetailsVwSQL(StringBuilder sb)
        {
            /*
            #region GetSelectDetailsVwSQL
            protected override string GetSelectDetailsVwSQL(Hashtable Parameters)
            {
                string ACTIVITY_ID = Parameters["ACTIVITY_ID"].ToString();
                
                string sql = SqlFile.GetSQL("GetSelectDetailsVwSQL", ACTIVITY_ID);

                return sql;
            }
            #endregion
             */
            sb.Append(IndentString("#region GetSelectDetailsVwSQL"));
            sb.Append("\n");

            sb.Append(IndentString("protected override string GetSelectDetailsVwSQL(Hashtable Parameters)"));
            sb.Append("\n");

            sb.Append(StartBlock);

            sb.Append(IndentString("throw new NotImplementedException();"));
            sb.Append("\n\n");

            sb.Append(IndentString("string sql = SqlFile.GetSQL(\"GetSelectDetailsVwSQL\");"));
            sb.Append("\n");
            sb.Append(IndentString("return sql;"));
            sb.Append("\n");

            sb.Append(EndBlock);

            sb.Append(IndentString("#endregion"));
            sb.Append("\n\n");
        }
        #endregion
        #region ExportGetInsertSQL
        private void ExportGetInsertSQL(StringBuilder sb)
        {
            /*
            #region GetInsertSQL
            public override List<string> GetInsertSQL(FESTIVAL_ACTIVITY aRec)
            {
                List<string> lst = new List<string>();

                string sql = SqlFile.GetSQL("GetInsertSQL", ACTIVITY_ID);

                lst.Add(sql);
             
                return lst;
            }
            #endregion
             */
            sb.Append(IndentString("#region GetInsertSQL"));
            sb.Append("\n");

            string FunctionPresentation = string.Format("public override List<string> GetInsertSQL({0} aRec)", Dal);
            sb.Append(IndentString(FunctionPresentation));
            sb.Append("\n");

            sb.Append(StartBlock);

            sb.Append(IndentString("throw new NotImplementedException();"));
            sb.Append("\n\n");

            sb.Append(IndentString("List<string> lst = new List<string>();"));
            sb.Append("\n");
            sb.Append(IndentString("string sql = SqlFile.GetSQL(\"GetInsertSQL\");"));
            sb.Append("\n");
            sb.Append(IndentString("lst.Add(sql);"));
            sb.Append("\n");
            sb.Append(IndentString("return lst;"));
            sb.Append("\n");

            sb.Append(EndBlock);

            sb.Append(IndentString("#endregion"));
            sb.Append("\n\n");
        }
        #endregion
        #region ExportGetUpdateSQL
        private void ExportGetUpdateSQL(StringBuilder sb)
        {
            /*
            #region GetUpdateSQL
            public override List<string> GetUpdateSQL(FESTIVAL_ACTIVITY aRec)
            {
                List<string> lst = new List<string>();

                string sql = SqlFile.GetSQL("GetUpdateSQL", ACTIVITY_ID);

                lst.Add(sql);
             
                return lst;
            }
            #endregion
             */
            sb.Append(IndentString("#region GetUpdateSQL"));
            sb.Append("\n");

            string FunctionPresentation = string.Format("public override List<string> GetUpdateSQL({0} aRec)", Dal);
            sb.Append(IndentString(FunctionPresentation));
            sb.Append("\n");

            sb.Append(StartBlock);

            sb.Append(IndentString("throw new NotImplementedException();"));
            sb.Append("\n\n");

            sb.Append(IndentString("List<string> lst = new List<string>();"));
            sb.Append("\n");
            sb.Append(IndentString("string sql = SqlFile.GetSQL(\"GetUpdateSQL\");"));
            sb.Append("\n");
            sb.Append(IndentString("lst.Add(sql);"));
            sb.Append("\n");
            sb.Append(IndentString("return lst;"));
            sb.Append("\n");

            sb.Append(EndBlock);

            sb.Append(IndentString("#endregion"));
            sb.Append("\n\n");
        }
        #endregion
        #region ExportGetDeleteSQL
        private void ExportGetDeleteSQL(StringBuilder sb)
        {
            /*
            #region GetDeleteSQL
            public override List<string> GetDeleteSQL(FESTIVAL_ACTIVITY aRec)
            {
                List<string> lst = new List<string>();

                string sql = SqlFile.GetSQL("GetDeleteSQL", ACTIVITY_ID);

                lst.Add(sql);
             
                return lst;
            }
            #endregion
             */
            sb.Append(IndentString("#region GetDeleteSQL"));
            sb.Append("\n");

            string FunctionPresentation = string.Format("public override List<string> GetDeleteSQL({0} aRec)", Dal);
            sb.Append(IndentString(FunctionPresentation));
            sb.Append("\n");

            sb.Append(StartBlock);

            sb.Append(IndentString("throw new NotImplementedException();"));
            sb.Append("\n\n");

            sb.Append(IndentString("List<string> lst = new List<string>();"));
            sb.Append("\n");
            sb.Append(IndentString("string sql = SqlFile.GetSQL(\"GetDeleteSQL\");"));
            sb.Append("\n");
            sb.Append(IndentString("lst.Add(sql);"));
            sb.Append("\n");
            sb.Append(IndentString("return lst;"));
            sb.Append("\n");

            sb.Append(EndBlock);

            sb.Append(IndentString("#endregion"));
            sb.Append("\n\n");
        }
        #endregion
        #region ExportCreateDbObject
        private void ExportCreateDbObject(StringBuilder sb)
        {
            /*
            #region CreateDbObject
            protected override IDatabase CreateDbObject()
            {
                return new FestivalTicketDatabase();
            }
            #endregion
             */
            sb.Append(IndentString("#region CreateDbObject"));
            sb.Append("\n");

            sb.Append(IndentString("protected override IDatabase CreateDbObject()"));
            sb.Append("\n");

            sb.Append(StartBlock);

            sb.Append(IndentString("throw new NotImplementedException();"));
            sb.Append("\n\n");

            string sCreateDatabase = string.Format("IDatabase database = new Database(DatabaseMode.{0}, \"{1}\");", DbSettings.DatabaseMode.ToString(), DbSettings.ConnectionString);
            sb.Append(IndentString(sCreateDatabase));
            sb.Append("\n");
            sb.Append(IndentString("return database;"));
            sb.Append("\n");

            sb.Append(EndBlock);

            sb.Append(IndentString("#endregion"));
            sb.Append("\n\n");
        }
        #endregion
    }
}
